<?php
namespace app\common\lib;
use think\facade\Cache;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CusSession
 */
class CusSession {
    //put your code here
    public static $sid = '';
    public static $send_flg = false;
    public static $expire = 24*3600;

    public static function set($key, $value, $expire = null){
        if(empty(self::$sid)){
            return ;
        }
        $data = Cache::get(self::$sid);
        if(is_null($value)){
            unset($data[$key]);
        }
        else{
            $data[$key] = $value;
        }
        if(is_null($expire)){
            $expire = self::$expire;
        }
        return Cache::set(self::$sid,$data,$expire);
    }

    public static function get($key = ''){
        if(empty(self::$sid)){
            return ;
        }
        $data = Cache::get(self::$sid);
        if($key == ''){
            return $data;
        }
        else{
            return isset($data[$key]) ? $data[$key] : '';
        }
    }

    public static function setId($sid = ''){
        if($sid){
            self::$sid = $sid;
            self::$send_flg = false;
        }
        else{
            self::$sid = self::guid();
            self::$send_flg = true;
        }

        $data = Cache::get($sid);
        if($data){
            $expire = self::$expire;;
            Cache::set($sid, $data, $expire);
        }
    }
    public static function guid(){
        if (function_exists('com_create_guid')){
            return com_create_guid();
        }else{
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            return strtoupper(md5(uniqid(rand(), true)));
        }
    }
}
